<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">实验室</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px;"
          placeholder="实验室"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <label class="el-form-item-label">试剂柜</label>
        <el-select
          v-model="query.cabinetId"
          clearable
          style="width: 185px;"
          placeholder="试剂柜"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in cabinets"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <!-- <el-form-item label="危化品类型" prop="type">
            <el-input v-model="form.type" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="计数类型" prop="countType">
            <el-select
              v-model="form.countType"
              style="width: 370px;"
              placeholder="请选择计数类型"
            >
              <el-option label="数量" :value="1" />
              <el-option label="重量" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item
            :label="form.countType == 1 ? '容量(ml)' : '容量(g)'"
            prop="amount"
          >
            <el-input v-model="form.amount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="实验室" prop="labId">
            <el-select
              v-model="form.labId"
              style="width: 370px;"
              clearable
              filterable
              :remote-method="getLabs"
              :loading="searchLoading"
              placeholder="请选择实验室"
              @change="onChanged"
            >
              <el-option
                v-for="item in labs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="试剂柜" prop="cabinetId">
            <el-select
              v-model="form.cabinetId"
              style="width: 370px;"
              clearable
              filterable
              :remote-method="getCabinets"
              :loading="searchLoading"
              placeholder="请选择试剂柜"
            >
              <el-option
                v-for="item in cabinets"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="试剂柜" prop="cabinetId">
            <el-input v-model="form.cabinetId" style="width: 370px;" />
          </el-form-item> -->
          <!-- <el-form-item label="计量方式" prop="measureId">
            未设置字典，请手动设置 Radio
          </el-form-item> -->
          <el-form-item label="危险等级" prop="dangerClasses">
            <el-select
              v-model="form.dangerClasses"
              style="width: 370px;"
              placeholder="请选择危险等级"
            >
              <el-option label="普通" :value="1" />
              <el-option label="危险" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              v-model="form.note"
              :rows="3"
              type="textarea"
              style="width: 370px;"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column label="级别" prop="grade" align="center"/>
        <el-table-column prop="labName" label="实验室" />
        <el-table-column prop="cabinetName" label="试剂柜" />
        <el-table-column prop="totalNumber" label="库存" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.totalNumber }}</span>
            <span>{{ scope.row.unit==3? 'g' :'mL'  }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="dangerClasses" width="155px" label="危险等级" align="center">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.dangerClasses == 1">一般试剂(I)</el-tag>
            <el-tag  v-if="scope.row.dangerClasses == 2">有机试剂(II)</el-tag>
            <el-tag type="danger" v-if="scope.row.dangerClasses == 3">剧毒试剂/易致毒试剂(III)</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          v-if="
            checkPer([
              'admin',
              'hazardousChemicals:edit',
              'hazardousChemicals:del',
            ])
          "
          label="操作"
          width="180px"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              class="isForbiddenBtn"
              @click="showDialog(scope.row)"
              >盘平</el-button
            >
          </template>
        </el-table-column>
        <!-- <el-table-column prop="labName" label="实验室" align="center" /> -->
        <!-- <el-table-column prop="cabinetName" label="试剂柜" align="center"/> -->
          <!-- <el-table-column prop="print" label="打印" align="center">
          <template slot-scope="scope">
            <button class="el-button el-button--primary el-button--mini">
              <i
                class="el-icon-printer"
                size="100"
                @click="fileDownload(scope.row.barCode)"
              ></i>
            </button>
          </template>
        </el-table-column>
        <el-table-column
          v-if="
            checkPer([
              'admin',
              'hazardousChemicals:edit',
              'hazardousChemicals:del'
            ])
          "
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column> -->
        </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <AddNumDialog v-if="isShowAddNumDialog" @refresh="crud.pageChangeHandler(0)" @close="isShowAddNumDialog = false"  :isShowAddNumDialog="isShowAddNumDialog" :type="type" :name="name"/>

  </div>
</template>

<script>
import crudHazardousChemicals,{getLabCabinetListAPI,searchIdAndType} from '@/api/hazardousChemicals'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { get } from '@/api/lab'
import { getCabinet } from '@/api/cabinet'
import { mapState } from 'vuex'
import AddNumDialog from "./components/addNumConsumableDialog.vue";

const defaultForm = {
  id: null,
  name: null,
  type: 3,
  amount: null,
  labId: null,
  note: null,
  createdAt: null,
  isDeleted: null,
  countType: null,
  updatedAt: null,
  barCode: null,
  cabinetId: null,
  measureId: 1,
  dangerClasses: null
}
export default {
  name: 'HazardousChemicalsStorehouse',
  components: { pagination, crudOperation, rrOperation, udOperation, AddNumDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '化学试剂盘库',
      url: 'api/hazardousChemicalsTotal/chemicalTotalList',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudHazardousChemicals },
      query: {
        type: 3
      },
      optShow: {
      add: false,
      edit: false,
      del: false,
      download: true,
      reset: true
    },
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'hazardousChemicals:add'],
        edit: ['admin', 'hazardousChemicals:edit'],
        del: ['admin', 'hazardousChemicals:del']
      },
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        // type: [
        //   { required: true, message: '危化品类型不能为空', trigger: 'blur' }
        // ],
        amount: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
        labId: [
          { required: true, message: '实验室不能为空', trigger: 'change' }
        ],
        countType: [
          { required: true, message: '计数类型不能为空', trigger: 'blur' }
        ],
        cabinetId: [
          { required: true, message: '试剂柜不能为空', trigger: 'change' }
        ],
        // measureId: [
        //   { required: true, message: '计量方式不能为空', trigger: 'blur' }
        // ],
        dangerClasses: [
          { required: true, message: '危险等级不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '名称' },
        // { key: 'type', display_name: '危化品类型' },
        { key: 'labName', display_name: '实验室' },
        { key: 'countType', display_name: '计数类型' },
        { key: 'cabinetName', display_name: '试剂柜' },
        // { key: 'measureId', display_name: '计量方式' },
        { key: 'dangerClasses', display_name: '危险等级' }
      ],
      searchLoading: false,
      labs: [],
      cabinets: [],
      searchForm: {
        page: 1,
        size: 1000,
        name: '',
        id: ''
      },
      isShowAddNumDialog:false,
      currentAddNumDialogInfo:[],
      name:'',
      type:''
    }
  },
  created() {
    this.getLabs()
    this.getCabinets()
  },
  async created() {
    // const res = await getStoreHouseINfoAPI(3)
    this.getLabCabinetList()
    this.getCabinets()
  },
  computed: {
    ...mapState('hjuser', ['systemSearchInfoId'])
  },
  methods: {
    async getLabCabinetList(){
      const res = await getLabCabinetListAPI()
      this.labs = res
    },
    getLabs(query = '') {
      this.searchForm.name = query
      this.searchLoading = true
      get()
        .then(response => {
          this.searchLoading = false
          this.labs = response
        })
        .catch(_ => {
          this.searchLoading = false
        })
    },
    getCabinets(query = '') {
      this.searchForm.name = query
      this.searchLoading = true
      getCabinet(this.searchForm)
        .then(response => {
          this.searchLoading = false
          this.cabinets = response.content
        })
        .catch(_ => {
          this.searchLoading = false
        })
    },
    onChanged(value) {
      this.searchForm.name = ''
      this.searchLoading = true
      getCabinet(this.searchForm)
        .then(response => {
          this.searchLoading = false
          this.cabinets = response.content
          this.cabinets = this.cabinets.filter(item => {
            return item.labId == value
          })
          if (this.cabinets.length != 0) {
            this.form.cabinetId = this.cabinets[0].id
          } else {
            this.form.cabinetId = null
          }
        })
        .catch(_ => {
          this.searchLoading = false
        })

      console.log(this.cabinets)
    },
    fileDownload(downloadUrl) {
      downloadUrl = 'data:image/jpeg;base64,' + downloadUrl
      console.log(downloadUrl)
      const aLink = document.createElement('a')
      aLink.style.display = 'none'
      aLink.href = downloadUrl
      aLink.download = '条形码.png'
      // 触发点击-然后移除
      document.body.appendChild(aLink)
      aLink.click()
      document.body.removeChild(aLink)
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async [CRUD.HOOK.afterRefresh]() {
      if (this.systemSearchInfoId) {
        try {
          const res = await searchIdAndType(this.systemSearchInfoId,3)
          this.crud.data = res.content
        } finally {
          this.$store.commit('hjuser/updatedsystemSearchInfoId', '')
        }
      }
      return true
    },
    showDialog(row){
      this.isShowAddNumDialog= true
      this.name = row.name
      this.type = row.type
    },
  }
}
</script>

<style scoped></style>
